<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap-4.4.1/css/bootstrap.min.css">
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="bootstrap-4.4.1/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="w-100">
    <div class="col-md-8" style="margin: 10px auto">
        <div class="form-group row">
            <label class="col-form-label col-3">用户名：</label>
            <input class="form-control col-8" type="text" id="userName">
        </div>
        <div class="form-group row">
            <label class="col-form-label col-3">密码：</label>
            <input class="form-control col-8" type="password" id="password">
        </div>
        <div class="row">
            <button class="btn btn-primary" onclick="save()">提交</button>
        </div>
    </div>
</div>

</body>
</html>
<script>
    var id = location.search.split("=")[1];

    $(function () {
        if (typeof id != 'undefined') {
            $.ajax({
                url: "http://localhost:8080/user/getById/" + id,
                success: function (data) {
                    if (data.code == 0) {
                        $("#userName").val(data.data.userName);
                        $("#password").val(data.data.password);
                    }
                }
            })
        }

    });
    function save() {
        console.log(id);
        var user = {
            "id": id,
            "userName": $("#userName").val(),
            "password": $("#password").val()
        };
        $.ajax({
            url: 'http://localhost:8080/user/save',
            method: 'post',
            contentType: 'application/json',
            data: JSON.stringify(user),
            success: function (data) {
                if (data.code == 0) {
                    alert("保存成功");
                    window.location.href = "list.html";
                } else {
                    alert(data.message);
                }
            }
        })
    }
</script>